<?php

function implode_with_key($assoc, $inglue = "='", $outglue = "', ") {
    $return = null;
    foreach ($assoc as $tk => $tv)
        $return.= $outglue . $tk . $inglue . $tv;
    return substr($return, 4);
}

function fecha($date, $hora = false) {
    if (empty($date) || strstr($date, "0000-00-00")) {
        return "No disponible";
    }
    $string = "d/m/Y";
    if ($hora) {
        $string = "d/m/Y H:i";
    }
    return date($string, strtotime($date));
}

function fechaSQL($fecha) {
    if (empty($fecha)) {
        return "";
    }
    $array = explode("/", $fecha);
    if (is_array($array) && count($array) == 3) {
        return $array[2] . "-" . $array[1] . "-" . $array[0];
    }
    return "";
}

function getCodigo($id) {
    return "#" . sprintf("%08d", $id);
}

function createPostData() {
    if (isset($_POST['data']['return_url'])) {
        $array = array();
    } else {
        $array = array("return_url" => $_SERVER['REQUEST_URI']);
    }
    if (isset($_POST['data'])) {
        foreach ($_POST['data'] as $modelName => $model) {
            if (is_array($model)) {
                foreach ($model as $campo => $valor) {
                    $array[$modelName . "][" . $campo] = $valor;
                }
            }
        }
    }
    return $array;
}

function generateUrl($action) {
    $array = array();
    $array['action'] = $action;
    if (isset($_POST['data']['return_url'])) {
        $array_return = explode("/", $_POST['data']['return_url']);
        foreach ($array_return as $part)
            if (strstr($part, "sort")) {
                $array['sort'] = str_replace("sort:", "", $part);
            } elseif (strstr($part, "direction")) {
                $array['direction'] = str_replace("direction:", "", $part);
            } elseif (strstr($part, "page")) {
                $array['page'] = str_replace("page:", "", $part);
            }
    }
    return $array;
}

function addQuotes($string) {
    return "'" . $string . "'";
}

function hasAction($field, $action) {
    if (!isset($field['actions'])) {
        return true;
    } elseif (isset($field['actions']) && strpos($field['actions'], $action) !== false) {
        return true;
    }
    return false;
}

function fieldsetVacio($fieldset, $action) {
    foreach ($fieldset as $field) {
        if (empty($field['actions']) || strpos($field['actions'], $action) !== false) {
            return false;
        }
    }
    return true;
}

function existePresentation($presentation, $path = '') {
    if (empty($presentation)) {
        return false;
    } elseif (strpos($presentation, "::") !== false) {
        list($path, $presentation) = explode("::", $presentation);
        if (file_exists(ROOT . '/app/Presentation/' . strtolower($path) . '/' . strtolower($presentation) . '.php')) {
            return ROOT . '/app/Presentation/' . strtolower($path) . '/' . strtolower($presentation) . '.php';
        }
    } elseif (file_exists(ROOT . '/app/Presentation/' . (empty($path) ? '' : $path . '/' ) . strtolower($presentation) . '.php')) {
        return ROOT . '/app/Presentation/' . (empty($path) ? '' : $path . '/' ) . strtolower($presentation) . '.php';
    } elseif (file_exists(ROOT . '/app/Presentation/default/' . strtolower($presentation) . '.php')) {
        return ROOT . '/app/Presentation/default/' . strtolower($presentation) . '.php';
    }
    return false;
}

function getModelAndField($name, $model) {
    if (strpos($name, ".") !== false) {
        return explode(".", $name);
    } else {
        return array($model, $name);
    }
}

function formatBytes($bytes, $precision = 2) {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');

    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);

    $bytes /= pow(1024, $pow);

    return round($bytes, $precision) . ' ' . $units[$pow];
}

